package com.zking.ssm.controller;

import com.github.pagehelper.Page;
import com.zking.ssm.model.SysUser;
import com.zking.ssm.service.ISysUserService;
import com.zking.ssm.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/SysUser")
public class SysUserController {

    @Autowired
    private ISysUserService iSysUserService;

    @RequestMapping("/add")
    public Map<String,Object> add(SysUser sysUser){
        int n = iSysUserService.insert(sysUser);
        Map<String,Object> map = new HashMap<>();
        if(n >= 0){
            map.put("code",0);
            map.put("message","增加系统用户成功！");
        }else{
            map.put("code",-1);
            map.put("message","增加系统用户失败！");
        }
        return map;
        }
    @RequestMapping("/del")
    public Map<String,Object> del(SysUser sysUser){
        System.out.println(sysUser);
        int n = iSysUserService.deleteByPrimaryKey(sysUser.getUserId());
        Map<String,Object> map = new HashMap<>();
        if(n > 0){
            map.put("code",0);
            map.put("message","删除系统用户成功！");
        }else{
            map.put("code",-1);
            map.put("message","删除系统用户失败！");
        }
        return map;
    }

    @RequestMapping("/update")
    public Map<String,Object> update(SysUser sysUser){
        int n = iSysUserService.updateUser(sysUser);
        Map<String,Object> map = new HashMap<>();
        if(n >= 0){
            map.put("code",0);
            map.put("message","修改系统用户成功！");
        }else{
            map.put("code",-1);
            map.put("message","修改系统用户失败！");
        }
        return map;
    }

    @RequestMapping("/login")
    public Map<String,Object> login(SysUser sysUser){
        System.out.println("前端传过来的参数"+sysUser);
        SysUser user = iSysUserService.login(sysUser);
        Map<String,Object> map = new HashMap<>();
        if(user != null){
            map.put("code",0);
            map.put("message","登陆成功！");
            map.put("request",user);
        }else{
            map.put("code",-1);
            map.put("message","登陆失败！");
        }
        return map;
    }


    @RequestMapping("/get")
    public Map<String,Object> get(SysUser sysUser){
        SysUser user = iSysUserService.selectByPrimaryKey(sysUser.getUserId());
        Map<String,Object> map = new HashMap<>();
        if(user != null){
            map.put("code",0);
            map.put("message","查询系统用户成功！");
            map.put("request",user);
        }else{
            map.put("code",-1);
            map.put("message","查询系统用户失败！");
        }
        return map;
    }



    @RequestMapping("/list")
    public Map<String,Object> list(SysUser sysUser, HttpServletRequest request){
        PageBean pageBean = new PageBean();
        System.out.println(pageBean);
        //初始化pagebean
        pageBean.setRequest(request);

        System.out.println("分页参数为"+pageBean);

        List<SysUser> list = iSysUserService.selectAll(sysUser, pageBean);
        Map<String,Object> map = new HashMap<>();
        if(list.size() >= 0){
            map.put("code",0);
            map.put("message","查询系统用户成功！");
            map.put("request",list);
            map.put("page",pageBean.getPage());//设置分页页码
            map.put("rows",pageBean.getRows());//设置分页页大小
            map.put("total",pageBean.getTotal());//设置分页数据总条数
        }else{
            map.put("code",-1);
            map.put("message","查询系统用户失败！");
        }
        return map;
    }



}
